

<!-- datagrid部分 -->
<table id="mail_list"></table>

<!-- 条件查询、按钮 -->
<form  id="mail_search">
	<table>
		<tr>
			<td>
				<label>标题：</label>
			</td>
			<td>
				<input    name="title" class="easyui-textbox"
				 data-options="prompt:'标题'" style="width:200px">
			</td>
			<td>
				<label>发送日期（之后）：</label>
			</td>
			<td>
				<input   name="sentDate_end" class="easyui-datebox" style="width:120px;">
			</td>
			<td>
				<a href="#" plain="true" id="mail_search_submit" onclick="javascript:;">查询</a>
				<a href="#" plain="true" id="mail_search_reset" onclick="javascript:;">重置</a>
			</td>
		</tr>
	</table>
	
	<div style="padding-top:2px;"></div>
	<a href="#" id="mail_add" onclick="javascript:;">创建任务</a>
    <a href="#" id="mail_view" onclick="javascript:;">查看</a>
</form>

<!-- 弹出窗口 detail -->
<div id="mail_popwin" class="easyui-dialog"
	style="width: 700px; height: 600px; padding: 10px 30px;"
	title="邮件信息" buttons="#mail_popwin_buttons"
	data-options="modal:true,closed:true">
	 <h3><label>邮件信息</label></h3>
     <form id="mail_detail_form" class="easyui-form" method="post">
     <input type="text" name="messageId"  id="messageId" style="width: 30px; display: none" />
		<table>
			<tr>
				<td>标题</td>
				<td><input  id="mail_subject" name="subject"  
					style="width: 550px;"  class="easyui-textbox"
					data-options="validType:'length[1,200]'"  /></td></tr>
			<tr>
				<td>发件人:</td>
				<td>  
				<input   id="mail_from"  name="from"    class="easyui-textbox"  
					style="width: 550px;"  ></input></td>
			</tr>
			<tr>
			<td>发送日期:</td>
				<td><input  id="mail_sentDate" name="sentDate"  
					style="width: 550px;"  class="easyui-textbox"/></td>	
			</tr>
			<tr>
				<td>正文:</td>
				<td><input   id="mail_content"  name="bodyText"   data-options="multiline:true,validType:'length[0,2000]'"  class="easyui-textbox" 
					style="width: 550px;height:260px;"  > </td>
			</tr>
				 
	      <tr>
				<td>附件:</td>
				<td><input  id="mail_attachments" name="attachments1"  
					style="width: 550px;"  class="easyui-tagbox" data-options="
						editable:false,
						multiple:true,
						valueField: 'id',
                		textField: 'text',
						limitToList:true,
		    			hasDownArrow:false,
						data: [ ]"  /></td>
			</tr>
			  
		</table>
     </form>
</div>
<div id="mail_popwin_buttons">
	<a href="#" id="mail_save" onclick="javascript:;">创建任务</a>
</div>
 
 
 
<div id="mailtask_popwin" class="easyui-dialog"
	style="width: 720px; height: 550px; padding: 10px 30px;"
	title="新建工作任务"  buttons="#mailtask_popwin_buttons"
	data-options="modal:true,closed:true">
<!-- 	 <h2><label>工作任务信息</label></h2> -->
     <form id="poptask_detail_form" class="easyui-form" method="post">
     <input type="text" name="id" style="width: 30px; display: none" />
		<table>
			<tr>
				<td>标题</td>
				<td><input   name="title"  id="poptask_title"
					style="width: 520px;"  class="easyui-textbox"
					data-options="validType:'length[1,200]',required:true"  /></td></tr>
			<tr>
				<td>内容:</td>
				<td><input   name="content"  id="poptask_content"
					style="width: 520px;height:200px" class="easyui-textbox"
					data-options="multiline:true,required:true,validType:'length[0,3000]'" /></td>
			</tr>
			<tr>
				<td>附件</td><td><input  id="poptask_attachments"   name="attachments"  
					style="width: 520px;" class="easyui-tagbox"  readonly="readonly" data-options="
						editable:false,
						multiple:true,
						valueField: 'id',
                		textField: 'text',
						limitToList:true,
		    			hasDownArrow:false,
						data: [ ]"  /></td> <!-- <td>   <a href="#" id="task_import" onclick="javascript:;">上传</a></td> --></tr>
		    <tr>
				<td>使用附件内容填充正文:</td>
				<td><select id="poptask_attTocontent" class="easyui-combobox" data-options="
				        valueField:'value',
                		textField:'value',
						limitToList:true,
		    			hasDownArrow:true"
					   style="width: 520px;"  ></select></td>
			</tr>	
			<tr>
				<td>办限时间:</td>
				<td><input     id="poptask_endDate"
					style="width: 520px;"  class="easyui-datebox"/></td></tr>
			
			<tr>
				<td>所属部门:</td>
				<td><select id="poptask_dept" class="easyui-combotree"    data-options="multiple: true"
					url="api/user/getL1DeptSubList" name="deptIds" style="width: 520px;"  ></select></td>
			</tr>
				<tr>
				<td>责任人:</td>
				<td><select id="poptask_doerCodes" class="easyui-combobox" data-options="
						valueField:'code',
                		textField:'name',
						limitToList:true,
						multiple: true,
		    			hasDownArrow:true"
					 name="doerCodes" style="width: 520px;"  ></select></td>
			</tr>
			<tr>
				<td>备注:</td> 
				<td><input   name="remark" style="width: 520px;" id="poptask_remark" 
					class="easyui-textbox" /></td>
			</tr>
			  
		</table>
     </form>
	</div>
	<div id="mailtask_popwin_buttons">
	<a href="#" id="poptask_save" onclick="javascript:;">保存</a>
	<a href="#" id="poptask_cancle" onclick="javascript:;">关闭</a>
</div>
<script type="text/javascript">
    
var  mail_search= function(){
	var modelname="Mail";
	var pageName="mail";
   	var listPage=$("#"+pageName+"_list");
   	var search_form=pageName+"_search"
   	var submit_button=$("#"+pageName+"_search_submit");
   	var reset_button=$("#"+pageName+"_search_reset");
  //以下根据id获取按钮元素
	var query_button=$("#"+pageName+"_query");
	var del_button=$("#"+pageName+"_del");
	var save_button=$("#"+pageName+"_save");
	var create_button=$("#"+pageName+"_add");
  	//Excel导入弹窗
   	var importWin=$("#"+pageName+"_import_popwin");
   	var importForm=$("#"+pageName+"_import_form");
   	var importFile=$("#"+pageName+"_import_file");
	//编辑弹窗
	var detailForm=$("#"+pageName+"_detail_form");
   	var popupWin=$("#"+pageName+"_popwin");
   	
	 listPage.datagrid({
	            url: 'api/mail/getMail',
	            singleSelect: true,
	            pagination: true,
	            rownumbers:true,
	            toolbar:"#"+search_form,
	            pageSize: 10,
	            pageList: [5, 10, 15, 20, 30, 50, 80, 200],
	            queryParams: form2Json(search_form),　　//关键之处
	            columns:[[{
		            field: "messageId",
		            title: "编号",
		            hidden: true
		        },{
		            field: "subject",
		            title: "标题",
		            align: 'center',
		            width: 650
		        }, 
		        {
		            field: "from",
		            title: "发件人",
		            align: 'center',
		            width: 150
		        }, 
		        {
		            field: "sentDate",
		            title: "发送时间",
		            align: 'center',
		            width: 150
		        }]], 
	            
	    		onDblClickRow:function(index,row){
	    			loadDetail();
	    		},
	    		onLoadError:function(data){
	    			if(data.responseText.indexOf("AuthenticationFailedException")>-1)
	    					$.messager.alert("系统提示","用户名或密码错误，邮箱登陆失败！");
	    			else
	    			$.messager.alert("系统提示",data.responseText);
	    		}
		        }); 
          
   

  submit_button.linkbutton({ iconCls: 'icon-search', plain: true })
    .click(function () {
    	//$.messager.alert("系统提示","暂不支持该方法");
        listPage.datagrid({ queryParams: form2Json(search_form) });   //点击搜索
    });
  reset_button.linkbutton({ iconCls: 'icon-clear', plain: true })
  .click(function () {
	  	$("#mail_search").form('reset');
  });   
	//删除一条记录
  del_button.linkbutton({ iconCls: 'icon-cut', plain: true })
  .click(function () {
  	var row = listPage.datagrid('getSelected');
		if (row){
			$.messager.confirm('系统提示','确认要删除么?',function(r){
			    if (r){
			        $.get('api/fileController/del',{finalName:row.finalName},function(data){
			        	var data = JSON.parse(data);
			        	$.messager.alert("系统提示",data.msg);
			        	listPage.datagrid("reload");					        	
			        });
			    }
			});		
		}else{
			$.messager.alert("系统提示","请鼠标点击选择要删除的行！");
			return;
		}
  });
  //生成新任务
  save_button.linkbutton({ iconCls: 'icon-ok', plain: true })
  .click(function () {
	  
	  popupWin.dialog("close");
	  $("#mailtask_popwin").dialog("open");
	  $("#poptask_save").linkbutton('enable');
	  var uid=$("#messageId").val();
   	  $("#poptask_content").textbox('setValue',$("#mail_content").textbox('getValue'));
   	  $("#poptask_title").textbox('setValue',$("#mail_subject").textbox('getValue'))
   	  $("#poptask_attachments").tagbox('setValues',$("#mail_attachments").tagbox('getValues'));
   	  $("#poptask_endDate").datebox('setValue',"");
   	  $("#poptask_dept").combotree('setValues',[]);
   	  $("#poptask_doerCodes").combobox('setValues',"");
   	  $("#poptask_remark").textbox('setValue',"");
   	
   	  var  values=$("#mail_attachments").tagbox('getValues');
   	  var valuesatt=[];
   	   
      $.each(values,function(index,value) {
    	  var ob=new Object();
    	  ob.value=$(value).html();
    	  if(ob.value.endWith(".doc")||ob.value.endWith(".docx")||ob.value.endWith(".wps")||ob.value.endWith(".txt")||ob.value.endWith(".pdf"))
    	  valuesatt.push(ob);
    			  }) ;
      $("#poptask_attTocontent").combobox({data:valuesatt});
  });
  
  //附件内容填充任务正文
  $('#poptask_attTocontent').combobox({
	    onChange: function(rec){
	    	var uid=$("#messageId").val();
	    	var fileName=rec;
	    	var url="./api/mail/getDocContent";
	    	 parent.$.messager.progress({
		            title : '提示',
		            text : '数据正在处理中，请稍后....'
		        });
	    	$.post(url,{fileName:fileName,uid:uid},function(data){
	    		parent.$.messager.progress('close');
	    		var result=JSON.parse(data);
	    		$('#poptask_content').textbox('setValue',result.data);
	    	});
	       }
	});
  
      String.prototype.endWith=function(str){
    	  if(str==null||str==""||this.length==0||str.length>this.length)
    	    return false;
    	  if(this.substring(this.length-str.length)==str)
    	    return true;
    	  else
    	    return false;
    	  return true;
    	  }
  $("#poptask_save").linkbutton({ iconCls: 'icon-ok', plain: true })
  .click(function () {
	  var isValid =$("#poptask_detail_form").form('validate');
      if(!isValid){
          return false;
      }
     $("#poptask_save").linkbutton('disable');
   	 $("#poptask_attachments").tagbox('getValues');
   	 $("#poptask_detail_form").form('submit',{
		url:'api/mail/mailTask',
		onSubmit:function(param){
			 parent.$.messager.progress({
		            title : '提示',
		            text : '数据正在处理中，请稍后....'
		        });
			param["messageId"]=$("#messageId").val();	
			if($("#poptask_dept").combobox('getText').length>0)
			param["deptNames"]=$("#poptask_dept").combobox('getText');
			if($("#poptask_doerCodes").combobox('getText').length>0)
			param["doerNames"]=$("#poptask_doerCodes").combobox('getText');
			if($("#poptask_endDate").datebox('getText').length>0)
			{ var temp= $("#poptask_endDate").datebox('getText').toString().split('-');	
			 param["endDate"]=moment({ years:temp[0], months:temp[1] - 1, date:temp[2]});	}
		},
		success:function(data){
			parent.$.messager.progress('close');
			var data = JSON.parse(data);
			$.messager.alert("系统提示",data.msg);
			$("#poptask_save").linkbutton('enable');
		}
	});
   
  });
	$("#poptask_cancle").linkbutton({ iconCls: 'icon-cancel', plain: true })
    .click(function () {  
    	 $('#mailtask_popwin').dialog('close');
    });
//弹出 数据导入窗口
  create_button.linkbutton({iconCls: 'icon-save', plain: true })
  .click(function () {
	  var row = listPage.datagrid('getSelected');
	  if(row)
	  loadDetail();
	  else
		  $.messager.alert("系统提示","请选择要查看的条目。。。");
	  
	  
	  
  });
  $("#mail_view").linkbutton({iconCls: 'icon-report', plain: true })
  .click(function () {
	  var row = listPage.datagrid('getSelected');
	  if(row)
	  loadDetail();
	  else
		  $.messager.alert("系统提示","请选择要查看的条目。。。");
  });
  
function loadDetail(){
	detailForm.form('reset');
	var row = listPage.datagrid('getSelected');
	//为了显示combobox，如果为null会报错
		detailForm.form("load",row);
		//$("#mail_content").texbox('setValue',row.bodyText);
		$.parser.parse(detailForm);
		popupWin.dialog("open");
		if(row.attachments!=null&&row.attachments.length>0)
			$("#mail_attachments").tagbox('setValues',row.attachments); 
		else 
			$("#mail_attachments").tagbox('setValues',[])
}
$('#poptask_dept').combotree({
	    onChange: function(rec){
	    	if(rec!=undefined&&rec.toString()!="")
	    	   var url = 'api/user/getDeptsUsers?deptIds='+rec;
	            $('#poptask_doerCodes').combobox('clear');
	            $('#poptask_doerCodes').combobox('reload', url);
	       }
	});
$("#mail_detail_form").delegate(".attachments","click",function(event){
	var uid=$("#messageId").val();
	var fileName=event.target.innerHTML;
	var url="./api/mail/getAttachment";
	params='?&fileName='+fileName+'&uid='+uid;
	window.location.href = url+ encodeURI(params);
	//window.open(url+ encodeURI(params));
});	
$("#poptask_detail_form").delegate(".attachments","click",function(event){
	debugger;
	var uid=$("#messageId").val();
	var fileName=event.target.innerHTML;
	var url="./api/mail/getAttachment";
	params='?&fileName='+fileName+'&uid='+uid;
	window.location.href = url+ encodeURI(params);
	//window.open(url+ encodeURI(params));
});	
function dateFormat(val, row){
		return moment(row.dkDate).format("YYYY-MM-DD");
}
//将表单数据转为json
function form2Json(id) {
    var arr = $("#" + id).serializeArray()
    var jsonStr = "";
    jsonStr += '{';
    for (var i = 0; i < arr.length; i++) {
    	if(arr[i].value!="")
        	jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",'
    }
    if(jsonStr.length>1)
    	jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
    jsonStr += '}'
    var json = JSON.parse(jsonStr)
    return json
}
 $(':input','#mail_detail_form')  
.not(':button, :submit, :reset')  
.val(' ')  
.attr('readonly','readonly');
 function ajaxLoading(){   
	    $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body");   
	    $("<div class=\"datagrid-mask-msg\"></div>").html("正在处理，请稍候。。。").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2});   
	 }   
	 function ajaxLoadEnd(){   
	     $(".datagrid-mask").remove();   
	     $(".datagrid-mask-msg").remove();               
	}  
}()
</script>